<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品编码" prop="goodsId">
        <el-input
          v-model="queryParams.skuId"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column show-overflow-tooltip label="SKU编码" align="center" prop="skuId" />
      <el-table-column show-overflow-tooltip label="规格名称" align="center" prop="spec" />
      <el-table-column label="SKU库存" align="center" prop="skuQuantity" />
      <el-table-column label="SKU预扣库存" align="center" prop="reserveQuantity" />
      <el-table-column label="SKU外部编码" align="center" prop="outerId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:goods:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:goods:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改拼多多商品列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品编码" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品总数量" prop="goodsQuantity">
          <el-input v-model="form.goodsQuantity" placeholder="请输入商品总数量" />
        </el-form-item>
        <el-form-item label="商品预扣库存" prop="goodsReserveQuantity">
          <el-input v-model="form.goodsReserveQuantity" placeholder="请输入商品预扣库存" />
        </el-form-item>
        <el-form-item label="商品图片" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入商品图片" />
        </el-form-item>
        <el-form-item label="是否多sku，0-单sku，1-多sku" prop="isMoreSku">
          <el-input v-model="form.isMoreSku" placeholder="请输入是否多sku，0-单sku，1-多sku" />
        </el-form-item>
        <el-form-item label="是否在架上，0-下架中，1-架上" prop="isOnsale">
          <el-input v-model="form.isOnsale" placeholder="请输入是否在架上，0-下架中，1-架上" />
        </el-form-item>
        <el-form-item label="商品缩略图" prop="thumbUrl">
          <el-input v-model="form.thumbUrl" placeholder="请输入商品缩略图" />
        </el-form-item>
        <el-form-item label="店铺id" prop="ownerId">
          <el-input v-model="form.ownerId" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="系统登录用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入系统登录用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pddGoodsSkuList } from "@/api/system/pddGoods";
export default {
  name: "GoodsDetail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼多多商品列表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        goodsName: null,
        goodsQuantity: null,
        goodsReserveQuantity: null,
        imageUrl: null,
        isMoreSku: null,
        isOnsale: null,
        thumbUrl: null,
        createdAt: null,
        ownerId: null,
        ownerName: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const goodId = this.$route.params && this.$route.params.goodId;
    this.queryParams.goodId = goodId;
    this.getList();
  },
  methods: {
    /** 查询拼多多商品列列表 */
    getList() {
      this.loading = true;
      pddGoodsSkuList(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsName: null,
        goodsQuantity: null,
        goodsReserveQuantity: null,
        imageUrl: null,
        isMoreSku: null,
        isOnsale: null,
        thumbUrl: null,
        createTime: null,
        ownerId: null,
        ownerName: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼多多商品列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改拼多多商品列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除拼多多商品列编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    },
    shouquan(){
      const url = "http://27.128.175.119/prod-api/pdd/pddCallback";
      const encodedUrl = encodeURIComponent(url);
      window.location.href = "https://fuwu.pinduoduo.com/service-market/auth?response_type=code&client_id=2ba77f6df16945b6860b9fbb3b9cf8ad&redirect_uri="+encodedUrl+"&state=1";
    },
    tongbu(){
      pddSyncPddData().then(response => {
        console.log(response)
      });
    }
  }
};
</script>
<style>

.item .el-form-item__label{
  margin-left: 14px;
}

</style>
